home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / cdbw.zip / SLISTBOX.C < prev    next >
Text File  |  1991-05-15  |  10KB  |  314 lines

  1. /*
  2.  *  SLISTBOX.C
  3.  *
  4.  *  This module contains listbox functions for SAMPLE.EXE.
  5.  *
  6.  *  Copyright (C) 1991 by Daytris.  All rights reserved.
  7.  */
  8.  
  9. #include <windows.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #ifndef ZORTECH
  14. #include <memory.h>
  15. #endif
  16. #include "dbmgr.h"
  17. #include "sampledb.h"
  18. #include "sample.h"
  19.  
  20.  
  21. /************************************************
  22.  * Local Data
  23.  ************************************************/
  24.  
  25.  
  26. /************************************************
  27.  * Function Declarations
  28.  ************************************************/
  29.  
  30. BOOL LoadClientListBox( HWND hWnd);
  31. BOOL AddToClientListBox( HWND hWnd, CLIENT *pClient);
  32. BOOL DeleteFromClientListBox( HWND hWnd, short nIndex);
  33. BOOL LoadAddressListBox( HWND hWnd);
  34. BOOL AddToAddressListBox( HWND hWnd, ADDRESS FAR *lpAddress);
  35. BOOL DeleteFromAddressListBox( HWND hWnd, short nIndex);
  36. HANDLE GetAddressHandle( HWND hWnd, short nIndex);
  37.  
  38.  
  39. /***************************************************************************
  40.  * Function : LoadClientListBox
  41.  *
  42.  * Purpose  : This function retrieves all client records from the
  43.  *            database and adds them to the client listbox.
  44.  *
  45.  * Returns  : TRUE  - load ok
  46.  *            FALSE - load not ok
  47.  ***************************************************************************/
  48. BOOL LoadClientListBox( HWND hWnd)
  49.     {
  50.     BOOL bFirstTime = TRUE;
  51.     DWORD dwStatus;
  52.     char szSortField[32];
  53.     CLIENT client;
  54.  
  55.     /* Reinitialize the listbox */
  56.     SendMessage( hWndClientLB, LB_RESETCONTENT, 0, 0L);
  57.  
  58.     /* Set key field string for search */
  59.     if( bSortByNumber)
  60.         strcpy( szSortField, "lClientNbr");
  61.     else
  62.         strcpy( szSortField, "szName");
  63.  
  64.     while( 1)
  65.         {
  66.         /* Get the client record */
  67.         if( bFirstTime)
  68.             {
  69.             bFirstTime = FALSE;
  70.             dwStatus = XDbRecordGetFirst( hDb, "client", szSortField,
  71.                 &client, sizeof( CLIENT));
  72.             }
  73.         else
  74.             {
  75.             dwStatus = XDbRecordGetNext( hDb, "client", szSortField,
  76.                 &client, sizeof( CLIENT));
  77.             }
  78.  
  79.         /* Check for errors */
  80.         if( dwStatus == E_NOTFOUND || dwStatus == E_NONEXT)
  81.             break;
  82.         if( dwStatus)
  83.             {
  84.             DbError( hWnd, dwStatus, __FILE__, __LINE__);
  85.             return FALSE;
  86.             }
  87.  
  88.         /* Add client to list box */
  89.         if( ! AddToClientListBox( hWnd, &client))
  90.             return FALSE;
  91.         }
  92.  
  93.     return TRUE;
  94.     }
  95.  
  96.  
  97. /***************************************************************************
  98.  * Function : AddToClientListBox
  99.  *
  100.  * Purpose  : This function adds a client record to the listbox.
  101.  *
  102.  * Returns  : TRUE  - add ok
  103.  *            FALSE - add not ok
  104.  ***************************************************************************/
  105. BOOL AddToClientListBox( HWND hWnd, CLIENT *pClient)
  106.     {
  107.     char szBuffer[64];
  108.  
  109.     if( bSortByNumber)
  110.         sprintf( szBuffer, "%ld   %s", pClient->lClientNbr,
  111.             pClient->szName);
  112.     else
  113.         sprintf( szBuffer, "%-30.30s   %ld", pClient->szName,
  114.             pClient->lClientNbr);
  115.  
  116.     if( SendMessage( hWndClientLB, LB_ADDSTRING, NULL,
  117.         (LONG)(LPSTR)szBuffer) == LB_ERR)
  118.         {
  119.         MessageBox( hWnd, "Error SendMessage / LB_ADDSTRING", "Fatal Error",
  120.             MB_OK | MB_ICONEXCLAMATION);
  121.         return FALSE;
  122.         }
  123.     return TRUE;
  124.     }
  125.  
  126.  
  127. /***************************************************************************
  128.  * Function : DeleteFromClientListBox
  129.  *
  130.  * Purpose  : This function deletes a string from the client listbox.
  131.  *
  132.  * Returns  : TRUE  - delete ok
  133.  *            FALSE - delete not ok
  134.  ***************************************************************************/
  135. BOOL DeleteFromClientListBox( HWND hWnd, short nIndex)
  136.     {
  137.     if( SendMessage( hWndClientLB, LB_DELETESTRING, nIndex, 0L) == LB_ERR)
  138.         {
  139.         return FALSE;
  140.         }
  141.     return TRUE;
  142.     }
  143.  
  144.  
  145. /***************************************************************************
  146.  * Function : LoadAddressListBox
  147.  *
  148.  * Purpose  : This function retrieves all member address records of
  149.  *            a selected client and adds them to the address listbox.
  150.  *            The address listbox contains a tab field that is not
  151.  *            in a visible area of the listbox.  The handle to the
  152.  *            address record is stored in this field in ASCII.
  153.  *
  154.  * Returns  : TRUE  - load ok
  155.  *            FALSE - load not ok
  156.  ***************************************************************************/
  157. BOOL LoadAddressListBox( HWND hWnd)
  158.     {
  159.     BOOL bFirstTime = TRUE;
  160.     HANDLE hAddress;
  161.     DWORD dwStatus;
  162.     ADDRESS FAR *lpAddress;
  163.  
  164.     /* Reinitialize the listbox */
  165.     SendMessage( GetDlgItem( hWnd, IDC_ADDR_LISTBOX), LB_RESETCONTENT, 0, 0L);
  166.  
  167.     while( 1)
  168.         {
  169.         /* Allocate space for the address record */
  170.         if( ! (hAddress = GlobalAlloc( GMEM_MOVEABLE | GMEM_DDESHARE,
  171.             (DWORD)sizeof( ADDRESS))) )
  172.             {
  173.             MessageBox( hWnd, "Out of memory", "Fatal Error",
  174.                 MB_ICONEXCLAMATION | MB_OK);
  175.             return FALSE;
  176.             }
  177.  
  178.         /* Get the first or next set connection.  Owner=client,
  179.            Member=address. */
  180.         if( bFirstTime)
  181.             {
  182.             bFirstTime = FALSE;
  183.             dwStatus = DbSetGetFirst( hDb, "client", "address", hAddress);
  184.             }
  185.         else
  186.             dwStatus = DbSetGetNext( hDb, "client", "address", hAddress);
  187.  
  188.         /* Check for errors */
  189.         if( dwStatus == E_NOTFOUND || dwStatus == E_NONEXT)
  190.             {
  191.             /* All members retrieved, break */
  192.             GlobalFree( hAddress);
  193.             break;
  194.             }
  195.         if( dwStatus)
  196.             {
  197.             DbError( hWnd, dwStatus, __FILE__, __LINE__);
  198.             return FALSE;
  199.             }
  200.  
  201.         /* Lock the address and add it to the listbox */
  202.         if( ! (lpAddress = (ADDRESS FAR *)GlobalLock( hAddress)) )
  203.             {
  204.             MessageBox( hWnd, "Memory: GlobalLock", "Fatal Error",
  205.                 MB_ICONEXCLAMATION | MB_OK);
  206.             return FALSE;
  207.             }
  208.         if( ! AddToAddressListBox( hWnd, lpAddress))
  209.             {
  210.             GlobalUnlock( hAddress);
  211.             return FALSE;
  212.             }
  213.         GlobalUnlock( hAddress);
  214.         }
  215.  
  216.     return TRUE;
  217.     }
  218.  
  219.  
  220. /***************************************************************************
  221.  * Function : AddToAddressListBox
  222.  *
  223.  * Purpose  : This function adds an address to the address listbox.
  224.  *
  225.  * Returns  : TRUE  - add ok
  226.  *            FALSE - add not ok
  227.  ***************************************************************************/
  228. BOOL AddToAddressListBox( HWND hWnd, ADDRESS FAR *lpAddress)
  229.     {
  230.     HANDLE hAddress;
  231.     LPSTR lpMem;
  232.     char szBuffer[64];
  233.  
  234.     /* Allocate memory and store Address record */
  235.     if( ! (hAddress = GlobalAlloc( GMEM_MOVEABLE, (DWORD)sizeof( ADDRESS))) )
  236.         {
  237.         MessageBox( hWnd, "Out of memory", "Fatal Error",
  238.             MB_ICONEXCLAMATION | MB_OK);
  239.         return FALSE;
  240.         }
  241.     if( (lpMem = GlobalLock( hAddress)) == NULL)
  242.         {
  243.         MessageBox( hWnd, "Memory: GlobalLock", "Fatal Error",
  244.             MB_ICONEXCLAMATION | MB_OK);
  245.         return FALSE;
  246.         }
  247.     _fmemcpy( lpMem, lpAddress, sizeof( ADDRESS));
  248.     GlobalUnlock( hAddress);
  249.  
  250.     /* Add the Address to the listbox.  Also store the handle to the ADDRESS
  251.        structure.  It will not be visible in the listbox. */
  252.     wsprintf( szBuffer, "%s\t%u", (LPSTR)lpAddress->szStreet, hAddress);
  253.     if( SendMessage( GetDlgItem( hWnd, IDC_ADDR_LISTBOX), LB_ADDSTRING,
  254.         NULL, (LONG)(LPSTR)szBuffer) == LB_ERR)
  255.         {
  256.         MessageBox( hWnd, "Error SendMessage / LB_ADDSTRING", "Fatal Error",
  257.             MB_ICONEXCLAMATION | MB_OK);
  258.         }
  259.  
  260.     return TRUE;
  261.     }
  262.  
  263.  
  264. /***************************************************************************
  265.  * Function : DeleteFromAddressListBox
  266.  *
  267.  * Purpose  : This function deletes an address from the address listbox.
  268.  *
  269.  * Returns  : TRUE  - delete ok
  270.  *            FALSE - delete not ok
  271.  ***************************************************************************/
  272. BOOL DeleteFromAddressListBox( HWND hWnd, short nIndex)
  273.     {
  274.     /* Get the handle to the address structure and free it */
  275.     if( GlobalFree( GetAddressHandle( hWnd, nIndex)) )
  276.         {
  277.         return FALSE;
  278.         }
  279.  
  280.     /* Delete the listbox entry */
  281.     if( SendMessage( GetDlgItem( hWnd, IDC_ADDR_LISTBOX), LB_DELETESTRING,
  282.         nIndex, 0L) == LB_ERR)
  283.         {
  284.         return FALSE;
  285.         }
  286.  
  287.     return TRUE;
  288.     }
  289.  
  290.  
  291. /***************************************************************************
  292.  * Function : GetAddressHandle
  293.  *
  294.  * Purpose  : This function retrieves the handle to an address record
  295.  *            of a selected string in the address listbox.
  296.  *
  297.  * Returns  : >0   - handle to address record
  298.  *            NULL - error
  299.  ***************************************************************************/
  300. HANDLE GetAddressHandle( HWND hWnd, short nIndex)
  301.     {
  302.     char szBuffer[64];
  303.  
  304.     /* Get the handle to the address structure and free it */
  305.     if( SendMessage( GetDlgItem( hWnd, IDC_ADDR_LISTBOX), LB_GETTEXT,
  306.         nIndex, (LONG)(LPSTR)szBuffer) == LB_ERR)
  307.         {
  308.         MessageBox( hWnd, "Error SendMessage / LB_GETTEXT", "Fatal Error",
  309.             MB_ICONEXCLAMATION | MB_OK);
  310.         return NULL;
  311.         }
  312.     return( atoi( strchr( szBuffer, '\t')));
  313.     }
  314.